package com.jnevision.policy.controller;


import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crtland.landoss.LandOss;
import com.crtland.landoss.Oss;
import com.crtland.landoss.OssInfo;
import com.crtland.landoss.OssType;
import com.crtland.landoss.PathEnum;
import com.crtland.landoss.Result;
import com.jnevision.common.CommonQuery;
import com.jnevision.policy.model.DocumentPolicy;
import com.jnevision.policy.service.DocumentPolicyServiceImpl;
import com.jnevision.util.DateUtil;


/**
 * <p>
 * 文件政策 前端控制器
 * </p>
 *
 * @author zzk
 * @since 2019-07-30
 */
@RestController
@RequestMapping("/policys")
public class DocumentPolicyController extends ApiController {

	
	@Autowired
	private DocumentPolicyServiceImpl documentPolicyServiceImpl;
	
	
	@Autowired
    private OssInfo ossInfo;
	
    @Autowired
    private StringRedisTemplate redisTemplate;
	
	@PostMapping
	public R<Boolean> save(HttpServletRequest request) {
		
			
		MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);  
	    MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");   
		
		String title = params.getParameter("title");
		String userName = params.getParameter("userName");
		String weight = params.getParameter("weight");
		String id = params.getParameter("id");
		String fileUrl = params.getParameter("fileUrl");
	    
	    
		DocumentPolicy documentPolicy = new DocumentPolicy();
		documentPolicy.setCreateTime(DateUtil.getNormalTime());		
		documentPolicy.setTitle(title);
		documentPolicy.setUserName(userName);
		documentPolicy.setWeight(Integer.valueOf(weight));
		if (StringUtils.isNotEmpty(id)) {
			documentPolicy.setId(Integer.valueOf(id));
		}
		if (StringUtils.isNotEmpty(fileUrl)) {
			documentPolicy.setFile(fileUrl);
		}else if (null != file) {
			try {
				LandOss landOss1 = new LandOss(redisTemplate);
				Oss oss1 = landOss1.getLandOss(OssType.ALI, ossInfo);
				Result pdfResult = oss1.upload(PathEnum.ENCLOSURE, file);
				if (pdfResult.getStatus()) {
					documentPolicy.setFile(pdfResult.getFileUrl());
				} else {
					return this.failed("文件上传失败，请重试");
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}	
		
		return this.success(this.documentPolicyServiceImpl.saveOrUpdate(documentPolicy));	
	}
	
	
	
	@GetMapping
	public R<IPage<DocumentPolicy>> page(Page<DocumentPolicy> page,CommonQuery commonQuery){
		
		return this.success(this.documentPolicyServiceImpl.page(page, new QueryWrapper<DocumentPolicy>()
				.between(StringUtils.isNotEmpty(commonQuery.getStart())&&StringUtils.isNotEmpty(commonQuery.getEnd()),
						"create_time" ,commonQuery.getStart(),commonQuery.getEnd()).like(StringUtils.isNotEmpty(commonQuery.getKeyword()), "title", commonQuery.getKeyword())));
	}
	
	
	@GetMapping("/{id}")
	public R<DocumentPolicy> get(@PathVariable String id){
		
		return this.success(this.documentPolicyServiceImpl.getById(id));
	}
	
	@DeleteMapping
	public R<Boolean> remove(String id){
		
		return this.success(this.documentPolicyServiceImpl.removeById(id));
	}
	
	
	
}

